<?php
function smarty_function_html($params, &$smarty) {
    
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
    $type = $params['type'];
    unset($params['type']);
    if($type == 'html'){
        $rs = smarty_function_html_html_html($params,$smarty);
    }elseif($type == 'slt'){
        $rs = smarty_function_html_html_select($params,$smarty);
    }elseif($type == 'txt'){
        $rs = smarty_function_html_html_txt($params,$smarty);
    }else{
        $rs = smarty_function_html_html_input($params,$smarty);
    }
    
    return $rs;
    
}

function smarty_function_html_html_input($params,&$smarty){
    
    $extra = '';
    foreach($params as $_key => $_val) {
        switch($_key) {
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    
    $rs = '<input type="input" '.$extra.'/>';
    
    return $rs;
    
}

function smarty_function_html_html_txt($params,&$smarty){
    $value = '';
    $extra = '';
    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'value':
                $$_key = (string)$_val;
                break;
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    
    
    $rs = '<textarea '.$extra.'>'.$value.'</textarea>';
    
    return $rs;
}

function smarty_function_html_html_html($params,&$smarty){
    
    $id = null;
    $value = '';

    $extra = '';
    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'id':
                $$_key = (string)$_val;
                break;
            case 'value':
                $$_key = (string)$_val;
                break;
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    
    if(empty($id)) return 'html 控件 id 必须！';
    
    $rs = '<textarea id="'.$id.'" '.$extra.'>'.$value.'</textarea>';
?>
<script type="text/javascript"> $(function(){
	$('#<?php echo $id;?>').xheditor({});
}); </script>

<?php     
    return $rs;
}

function smarty_function_html_html_select($params,&$smarty){
    
    $opt = array();
    $label = '';

    $extra = '';
    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'opt':
                $$_key = (array)$_val;
                break;
            case 'label':
                $$_key = (string)$_val;
                break;
            case 'value':
                $$_key = $_val;
                break;
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    
    $rs = '<select  '.$extra.'>';
    
    if(!empty($label)){
        $rs .= '<option value="">'.$label.'</option>';
    }
    
    foreach($opt as $k=>$v){
        $selected = '';
        if(isset($value) && $k == $value){
            $selected = ' selected="selected" ';
        }
        $rs .= '<option value="'.$k.'" '.$selected.'>'.$v.'</option>';
    }
    
    $rs .= '</select>';
    return $rs;    
    
}

function smarty_function_html_html_complent_id(&$comid=1){
    return $comid++;
}



